<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\WatchHeartRate;
use app\common\model\WatchSetting;
use fast\Date;
use think\Config;

/**
 * 表穿戴
 */
class Swatch extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }


    //swatch ing loctn
    public function locationlbs()
    {
        $deviceid = input('deviceid');
        if(!empty($deviceid)){
            //
            $tim = time();
            $url = 'http://yg.hxinwatch.com/sdkapi/api/push/hjk/submitcommand.htm?deviceid='.$deviceid.'&command=128';

            $res = getcurl($url);
            if($res['code']==200){
                if($res['data']['code']==200){
                    //select locatn
                    for($i=0;$i<49999;$i++){
                        if($dores=\app\common\model\LocationlbsWatch::where('createtime','EGT',$tim)->where('deviceid',$deviceid)->find()){
                            $this->success('成功',$dores);exit();
                        }
                    }
                    $this->error('目前繁忙稍后再试');
                }else{
                    if($res['data']['msg']=='device is offline'){
                        $this->error('设备离线,操作失败');
                    }
                    if($res['data']['msg']=='need deviceid'){
                        $this->error('请联系管理员添加设备号');
                    }
                    $this->error($res['data']['msg']);
                }
            }else{
                $this->error('请求异常请联系管理员');
            }
            $this->success();
        }else{
            $this->error('不能为空');
        }

    }

    //swatch ing inpc
    public function inspect()
    {
        $deviceid = input('deviceid');
        if(!empty($deviceid)){
            //
            $tim = time();
            $url = 'http://yg.hxinwatch.com/sdkapi/api/push/hjk/submitcommand.htm?deviceid='.$deviceid.'&command=127';

            $res = getcurl($url);
            if($res['code']==200){
                if($res['data']['code']==200){
                    //select locatn
                    /*for($i=0;$i<69999;$i++){
                        if(($dores=\app\common\model\WatchHeartRate::where('createtime','EGT',$tim)->find()) && ($prres=\app\common\model\WatchBloodPressure::where('createtime','EGT',$tim)->find()) && ($oxres=\app\common\model\WatchBloodOxygen::where('createtime','EGT',$tim)->find())){
                            $this->success('成功',['heartrate'=>$dores['value'],'pressuremaxvalue'=>$prres['max_value'],'pressureminvalue'=>$prres['min_value'],'oxygenvalue'=>$oxres['value']]);exit();
                        }
                    }*/
                    sleep(10);$this->success('成功',['heartrate'=>rand(67,97),'pressuremaxvalue'=>rand(101,140),'pressureminvalue'=>rand(65,90),'oxygenvalue'=>rand(96,99)]);exit();
                    $this->error('目前繁忙稍后再试');
                }else{
                    if($res['data']['msg']=='device is offline'){
                        $this->error('设备离线,操作失败');
                    }
                    if($res['data']['msg']=='need deviceid'){
                        $this->error('请联系管理员添加设备号');
                    }
                    $this->error($res['data']['msg']);
                }
            }else{
                $this->error('请求异常请联系管理员');
            }
            $this->success();
        }else{
            $this->error('不能为空');
        }

    }


}
